# Contributor: Isaac Dunham <ibid.ag@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Isaac Dunham <ibid.ag@gmail.com>
pkgname=openblas
_pkgname=OpenBLAS
pkgver=0.3.10
pkgrel=1
pkgdesc="A fast BSD-licensed BLAS based on gotoBLAS2, with LAPACK"
url="http://www.openblas.net"
arch="all !mips !mips64"
license="BSD-3-Clause"
replaces="blas lapack"
depends_dev="gfortran"
makedepends="$depends_dev perl linux-headers"
subpackages="$pkgname-static $pkgname-dev $pkgname-doc"
source="$pkgname-$pkgver.tar.gz::https://github.com/xianyi/$_pkgname/archive/v$pkgver.tar.gz
	"
builddir="$srcdir/$_pkgname-$pkgver"

_builddir64="$builddir-64"

# ILP64 is not supported on aarch64 (https://github.com/xianyi/OpenBLAS/issues/956)
_ilp64=false
if [ "$CARCH" = "x86_64" ]; then
	subpackages="$subpackages $pkgname-ilp64"
	_ilp64=true
fi

# USE_OPENMP=0: see GCC bug 60035
# NO_AFFINITY: don't expect to work on musl, also breaks R and not
#              recommended for Julia
_flags="USE_OPENMP=0
	NO_LAPACK=0
	MAJOR_VERSION=3
	NO_AFFINITY=1"
case "$CARCH" in
ppc64le) _flags="$_flags NUM_THREADS=64";;  # POWER machines are big
*64|s390x) _flags="$_flags NUM_THREADS=32";;  # match kernel's CONFIG_NR_CPUS
*) _flags="$_flags NUM_THREADS=8";;  # 32bit machines usually have less cores
esac

# DYNAMIC_ARCH is supported on x86* only, see https://github.com/xianyi/OpenBLAS/issues/709
[ "${CARCH%%_64*}" = "x86" ] && _flags="$_flags DYNAMIC_ARCH=1" || _flags="$_flags DYNAMIC_ARCH=0"
case "$CARCH" in
x86*) _flags="$_flags TARGET=CORE2";;
armhf) _flags="$_flags TARGET=ARMV7";;
aarch64) _flags="$_flags TARGET=ARMV8";;
ppc64le) _flags="$_flags TARGET=POWER8";;
s390x) _flags="$_flags TARGET=ZARCH_GENERIC";;
esac

# Flags for ILP64 variant.
_flags64="$_flags
	INTERFACE64=1
	SYMBOLSUFFIX=64_"

prepare() {
	default_prepare

	if [ "$CARCH" = "ppc64le" ] ; then
		# Skip test level 3 on ppc64le for a while
		sed 's/level2 level3/level2/g' -i "$builddir"/test/Makefile
		sed 's/all2 all3/all2/g' -i "$builddir"/ctest/Makefile
	fi
	cp -ar "$builddir" "$_builddir64"
}

build() {
	CFLAGS= make -C "$builddir" \
		$_flags CFLAGS="$CFLAGS" PREFIX=/usr

	if $_ilp64; then
		msg "Building with ILP64..."
		CFLAGS= make -C "$_builddir64" \
			$_flags64 CFLAGS="$CFLAGS" PREFIX=/usr
	fi
}

dev() {
	replaces="blas-dev lapack-dev"
	default_dev
}

check() {
	CFLAGS= make $_flags CFLAGS="$CFLAGS" tests
}

package() {
	if $_ilp64; then
		make -C "$_builddir64" $_flags64 \
			PREFIX="$pkgdir/usr" install
	fi

	make $_flags PREFIX="$pkgdir/usr" install

	# Remove reference to $pkgdir.
	sed -e "s|$pkgdir||" \
		-i "$pkgdir"/usr/lib/cmake/$pkgname/OpenBLASConfig.cmake \
		-i "$pkgdir"/usr/lib/pkgconfig/openblas.pc

	mkdir -p "$pkgdir"/usr/share/doc/$pkgname
	install -m 0644 Changelog.txt TargetList.txt USAGE.md \
		"$pkgdir"/usr/share/doc/$pkgname/

	install -Dm 0644 LICENSE \
		"$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}

ilp64() {
	pkgdesc="$pkgdesc (ILP64)"

	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libopenblas64_* "$subpkgdir"/usr/lib/
}

sha512sums="64a5f983b2f6e02cdb6e0f14433498cc5daa1ccfb49246f7a2dcd38f9982fa608f2abea069fe0e35012af8c1441c43d1f6418eaccd40795f5002fed1c36ce05d  openblas-0.3.10.tar.gz"
